﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN32_3V : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {            
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
            this.SetControl();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }    
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN32_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnLoanObjective.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/VmLoanObjective.aspx") + "','700','550');");

        txtLoanObjective.AddControl(hidLoanObjective.ClientID);
        txtLoanObjective.AddControl(txtLoanObjective.ClientID);
        txtLoanObjective.ControlSearch = txtLoanObjective.ClientID;
        txtLoanObjective.AddQueryString("bIsActive", "1");
        txtLoanObjective.SearchKey = "VmLoanObjective_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";


        btnArrangementPurpose.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ArrangementPurpose.aspx") + "','700','550');");

        txtArrangementPurpose.AddControl(hidLoanObjective.ClientID);
        txtArrangementPurpose.AddControl(txtLoanObjective.ClientID);
        txtArrangementPurpose.ControlSearch = txtLoanObjective.ClientID;
        txtArrangementPurpose.AddQueryString("bIsActive", "1");
        txtArrangementPurpose.SearchKey = "ArrangementPurpose_oArrangementPurposeID_oArrangementPurposeName";
    }
    private void BindData(int ID)
    {
        List<HPAndTFAddOn> hPAndTFAddOn = new List<HPAndTFAddOn>();
        DataTable dt = new SPR_HPAndTFAddOn().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, hPAndTFAddOn);

             txtTransactionDate.Text = DateTime.Now.DateToString();
            ddlSourceData.SelectedValue = hPAndTFAddOn[0].SourceDataID.ToString();
            txtAccountNo.Text = hPAndTFAddOn[0].AccountNo;
            txtRefinanceDate.Text = hPAndTFAddOn[0].RefinanceDate.DateToString();
            txtMisPaymentDate.Text = hPAndTFAddOn[0].MisPaymentDate.DateToString();
            txtLoanObjective.Text = hPAndTFAddOn[0].LoanObjectiveName;
            txtArrangementPurpose.Text = hPAndTFAddOn[0].ArrangementPurposeName;
            ddlCreditLevel.SelectedValue = hPAndTFAddOn[0].CreditLevelID.ToString();
            ddlDoubtfulStatus.SelectedValue = hPAndTFAddOn[0].DoubtfulStatus.ToString();
            ddlOperationStatus.SelectedValue = hPAndTFAddOn[0].OperationStatus.ToString();
            ddlTDRType.SelectedValue = hPAndTFAddOn[0].TDRTypeID.ToString();
            ddlChangeCondition.SelectedValue = hPAndTFAddOn[0].ChangeConditionID.ToString();
            txtOldAccountNo.Text = hPAndTFAddOn[0].OldAccountNo;
            txtReservedAmount.Text = hPAndTFAddOn[0].ReservedAmount.ToString();
            txtUnpaidInterest.Text = hPAndTFAddOn[0].UnpaidInterest.ToString();   
        }
    }
    private void BindDDL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
        ddlCreditLevel.SetData(DDL_TYPE.CreditLevel, "", false, true);
        ddlDoubtfulStatus.SetData(DDL_TYPE.DoubtfulStatus, "", false, true);
        ddlOperationStatus.SetData(DDL_TYPE.OperationStatus, "", false, true);
        ddlTDRType.SetData(DDL_TYPE.TDRType, "", false, true);
        ddlChangeCondition.SetData(DDL_TYPE.ChangeCondition, "", false, true);
    }
    private void SetControl()
    {
        SetControlReadOnly(form_control);
    }
    protected void SetControlReadOnly(PlaceHolder plcReadOnly)
    {
        foreach (Control ctrl in plcReadOnly.Controls)
        {
            if (ctrl is Button)
            {
                ((Button)ctrl).Enabled = false;
            }
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Attributes["readOnly"] = "true";
                ((TextBox)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is DropDownList)
            {
                ((DropDownList)ctrl).Enabled = false;
                ((DropDownList)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is RadioButton)
            {
                ((RadioButton)ctrl).Attributes["readOnly"] = "true";
                ((RadioButton)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is Image)
            {
                ((Image)ctrl).Attributes["disabled"] = "true";
            }
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN32_3V;
    }
    #endregion
}